package com.raining.inningz.ui.play;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.raining.inningz.R;
import com.raining.inningz.adapter.RulesListAdapter;
import com.raining.inningz.baseclass.BaseFragment;
import com.raining.inningz.constants.IFragmentNameConstants;
import com.raining.inningz.constants.IParamConstants;
import com.raining.inningz.custom.MyListView;
import com.raining.inningz.database.DbManager;
import com.raining.inningz.database.dao.ScoringTemplates;
import com.raining.inningz.database.dao.ScoringTemplatesDao.Properties;
import com.raining.inningz.manager.NotifyManager;
import com.raining.inningz.manager.SelectorManager;

public class PlayRulesFragment extends BaseFragment implements OnClickListener {

	private LayoutInflater mInflater;
	private View view;

	private LinearLayout linContainer;
	private LinearLayout linContainerRulesDisplay;
	private LinearLayout linContainerRulesDisplayCustom;

	private MyListView lstRules;

	RulesListAdapter rulesListAdapter;

	
	
	private List<ScoringTemplates> scoringTemplates;
	private PlayMatchDetailsFragment playMatchDetailsFragment;
	
	
	
	private String scoreTempletID="";

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			
			
			matchID=getArguments().getString(IParamConstants.MATCH_ID);
			
			isEdit=getArguments().getBoolean(IParamConstants.IS_EDIT);
			}
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mInflater = inflater;

		view = inflater.inflate(R.layout.fragment_play_rules, container, false);
		context = mInflater.getContext();

		
			
		
		initialiseUI();

		setListner();

		setListData();

		return view;
	}
	
	

	private void initialiseUI() {

		setHeaderIntialize(view);
		setLeftReturn();
		setLeftTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "InningZ");
		setRightTextSelector(resources.getColor(R.color.top_bar_Button),
				resources.getColor(R.color.top_bar_Button_press), "Save");
		setHeaderTitle(resources.getString(R.string.about), Color.BLACK);

		linContainer = (LinearLayout) view.findViewById(R.id.linContainer);
		linContainerRulesDisplay = (LinearLayout) view
				.findViewById(R.id.linContainerRulesDisplay);
		linContainerRulesDisplayCustom = (LinearLayout) view
				.findViewById(R.id.linContainerRulesDisplayCustom);

		lstRules = (MyListView) view.findViewById(R.id.lstRules);
		daoSession = DbManager.getDbManager(context);

	}

	private void setListner() {

		getBtnLeft().setOnClickListener(this);
		getBtnRight().setOnClickListener(this);

		lstRules.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {

				setRuleData(pos);

			}
		});

	}

	private void setRuleData(int pos) {

		if ((scoringTemplates.size() > 0) && (pos >= 0)) {

			View convertView;
			ScoringTemplates preRulesTable = scoringTemplates.get(pos);

			
			
			TextView txtipt;// Common

			RelativeLayout relTitle;// Common
			TextView txtTitle;// Common

			TextView txtopt;
			TextView txtopbpi;
			TextView txtebfnb;
			TextView txtprfnb;
			TextView txtafh;
			TextView txtebfw;
			TextView txtprfw;
			TextView txtabr;
			TextView txtapr;
			TextView txtbptpi;

			TextView txtbpbpi; // Common

			TextView txtbpo;

			EditText edtopt;
			EditText edtopbpi;
			EditText edtprfnb;
			EditText edtprfw;
			EditText edtbptpi;
			EditText edtbpo;

			ToggleButton tgleebfnb;
			ToggleButton tgleafh;
			ToggleButton tgleebfw;
			ToggleButton tgleabr;
			ToggleButton tgleapr;

			if (preRulesTable.getTemplateName().equalsIgnoreCase("Custom")) {

				convertView = mInflater.inflate(
						R.layout.view_rules_display_custom, null);

				edtopt = (EditText) convertView.findViewById(R.id.edtopt);
				edtopbpi = (EditText) convertView.findViewById(R.id.edtopbpi);
				edtprfnb = (EditText) convertView.findViewById(R.id.edtprfnb);
				edtprfw = (EditText) convertView.findViewById(R.id.edtprfw);
				edtbptpi = (EditText) convertView.findViewById(R.id.edtbptpi);
				edtbpo = (EditText) convertView.findViewById(R.id.edtbpo);

				tgleebfnb = (ToggleButton) convertView
						.findViewById(R.id.tgleebfnb);
				tgleafh = (ToggleButton) convertView.findViewById(R.id.tgleafh);
				tgleebfw = (ToggleButton) convertView
						.findViewById(R.id.tgleebfw);
				tgleabr = (ToggleButton) convertView.findViewById(R.id.tgleabr);
				tgleapr = (ToggleButton) convertView.findViewById(R.id.tgleapr);

				SelectorManager.setBackground(tgleebfnb, SelectorManager
						.getToggleButtonSelection(context,
								R.drawable.switch_on, R.drawable.switch_off));
				SelectorManager.setBackground(tgleafh, SelectorManager
						.getToggleButtonSelection(context,
								R.drawable.switch_on, R.drawable.switch_off));
				SelectorManager.setBackground(tgleebfw, SelectorManager
						.getToggleButtonSelection(context,
								R.drawable.switch_on, R.drawable.switch_off));
				SelectorManager.setBackground(tgleabr, SelectorManager
						.getToggleButtonSelection(context,
								R.drawable.switch_on, R.drawable.switch_off));
				SelectorManager.setBackground(tgleapr, SelectorManager
						.getToggleButtonSelection(context,
								R.drawable.switch_on, R.drawable.switch_off));

				tgleebfnb.setChecked(preRulesTable.getExtraBallForNoBall());
				tgleafh.setChecked(preRulesTable.getAllowFreehit());
				tgleebfw.setChecked(preRulesTable.getExtraBallForWide());
				tgleabr.setChecked(preRulesTable.getAllowBonus());
				tgleapr.setChecked(preRulesTable.getAllowPenalties());

				tgleebfnb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(CompoundButton arg0,
									boolean arg1) {
								// TODO Auto-generated method stub

							}
						});

				tgleafh.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(CompoundButton arg0,
									boolean arg1) {
								// TODO Auto-generated method stub

							}
						});
				tgleebfw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(CompoundButton arg0,
									boolean arg1) {
								// TODO Auto-generated method stub

							}
						});
				tgleabr.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(CompoundButton arg0,
									boolean arg1) {
								// TODO Auto-generated method stub

							}
						});
				tgleapr.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(CompoundButton arg0,
									boolean arg1) {
								// TODO Auto-generated method stub

							}
						});

				edtopt.setText(preRulesTable.getOversPerInning().toString());
				edtopbpi.setText(preRulesTable.getOverPerBowler()
						.toString());
				edtprfnb.setText(preRulesTable.getRunsPerNoBall()
						.toString());
				edtprfw.setText(preRulesTable.getRunsPerWide()
						.toString());
				edtbptpi.setText(preRulesTable.getBatsmanPerInnings()
						.toString());
				edtbpo.setText(preRulesTable.getBallsPerOver().toString());

			} else {
				
				scoreTempletID=preRulesTable.getId().toString();
				
				
				convertView = mInflater.inflate(R.layout.view_rules_display,
						null);

				txtopt = (TextView) convertView.findViewById(R.id.txtopt);
				txtopbpi = (TextView) convertView.findViewById(R.id.txtopbpi);
				txtebfnb = (TextView) convertView.findViewById(R.id.txtebfnb);
				txtprfnb = (TextView) convertView.findViewById(R.id.txtprfnb);
				txtafh = (TextView) convertView.findViewById(R.id.txtafh);
				txtebfw = (TextView) convertView.findViewById(R.id.txtebfw);
				txtprfw = (TextView) convertView.findViewById(R.id.txtprfw);
				txtabr = (TextView) convertView.findViewById(R.id.txtabr);
				txtapr = (TextView) convertView.findViewById(R.id.txtapr);
				txtbptpi = (TextView) convertView.findViewById(R.id.txtbptpi);
				txtbpo = (TextView) convertView.findViewById(R.id.txtbpo);

				txtopt.setText(preRulesTable.getOversPerInning().toString());
				txtopbpi.setText(preRulesTable.getOverPerBowler()
						.toString());
				txtebfnb.setText((preRulesTable.getExtraBallForNoBall()) ? "Yes"
						: "NO");
				txtprfnb.setText(preRulesTable.getRunsPerNoBall()
						.toString());

				txtafh.setText((preRulesTable.getAllowFreehit()) ? "Yes"
						: "NO");
				txtebfw.setText((preRulesTable.getExtraBallForWide()) ? "Yes"
						: "NO");
				txtprfw.setText(preRulesTable.getRunsPerWide()
						.toString());
				txtabr.setText((preRulesTable.getAllowBonus()) ? "Yes"
						: "NO");
				txtapr.setText((preRulesTable.getAllowPenalties()) ? "Yes"
						: "NO");
				txtbptpi.setText(preRulesTable.getBatsmanPerInnings()
						.toString());
				txtbpo.setText(preRulesTable.getBallsPerOver().toString());

			}

			txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
			txtipt = (TextView) convertView.findViewById(R.id.txtipt);
			txtbpbpi = (TextView) convertView.findViewById(R.id.txtbpbpi);

			relTitle = (RelativeLayout) convertView.findViewById(R.id.relTitle);
			relTitle.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					
					scoreTempletID="";
					lstRules.setVisibility(View.VISIBLE);
					linContainer.removeAllViews();
				}
			});

			txtTitle.setText(preRulesTable.getTemplateName().toString());
			txtipt.setText(preRulesTable.getInningsPerTeam().toString());

			txtbpbpi.setText(preRulesTable.getBallsPerBatsman()
					.toString());
			lstRules.setVisibility(View.GONE);
			linContainer.removeAllViews();
			linContainer.addView(convertView);

		}

	}

	private void setListData() {

		scoringTemplates = scoringTemplatesDao.queryBuilder().where(Properties.IsTemplatePredefined.eq(true)).list();

		rulesListAdapter = new RulesListAdapter(context, scoringTemplates);
		lstRules.setAdapter(rulesListAdapter);

	}

	@Override
	public void onClick(View v) {

		if (v == getBtnLeft()) {
			fragmentManager.popBackStack();
		} else if (v == getBtnRight()) {
			
			if(!scoreTempletID.equals("")){
			
			Bundle args = new Bundle();
			args.putString(IParamConstants.MATCH_ID, matchID);
			args.putBoolean(IParamConstants.IS_EDIT, true);
			playMatchDetailsFragment=new PlayMatchDetailsFragment();
			
			playMatchDetailsFragment.setArguments(args);
			changeFragment(playMatchDetailsFragment, IFragmentNameConstants.PLAY_MATCH_DETAILS);
			}
			else{
				NotifyManager.showShortToast(context, resources.getString(R.string.rule_err));
			}
		}

	}

}
